-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[8.x] Add upsert to Eloquent and Base Query Builders #34698
Conversation
Usage of Reference: https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
The sample below is from the link above showcasing the new syntax to use: INSERT INTO t1 (a,b,c) VALUES (1,2,3),(4,5,6) AS new
ON DUPLICATE KEY UPDATE c = new.a+new.b; |
@rodrigopedra does that new syntax work in MySQL 5.7? Though I guess it doesn't really matter as this feature could require MySQL 8.0. |
Old syntax works on both MySQL versions (5 and 8). New syntax was introduced in MySQL 8.0.19, from the link on my previous message:
To support both versions I think we can keep the old syntax around as the docs says: "...and is subject to removal in a future version of MySQL". No removal timeline was set yet. In general it takes a while to MySQL really remove deprecated features (for example the So we could use the old syntax for now and handle this case when it actually gets removed. EDIT: corrected the deprecation example about |
Yeah, it's still supported but deprecated beginning 8.0.20. It isn't removed yet and will be around for a while. So to support both mySQL 5 and 8, it's best to continue using Besides, we're already using |
Actually @paras-malhotra, the usage of What is deprecated is the usage of the https://dev.mysql.com/doc/refman/8.0/en/miscellaneous-functions.html#function_values |
Ohh wow, I didn't know that 😮. Okay, I'll probably look out for the removal notice since row/column aliases don't work in 5.7. @taylorotwell if you'd like I can implement the new way (row/column aliases), but I'd recommend we go with |
No problem, maybe one of the reasons to deprecate that function is the confusion with the SQL clause usage. MariaDB took a similar step, but they renamed the I think this feature is a great addition to the framework and that for new it is best to keep the |
Great addition! This is the first I add to new projects myself. However, this PR misses the option to do partial updates. You don't want to keep overwriting e.g. @paras-malhotra do you know if partial updates is supported by other than MySQL and PostgreSQL? Thanks for contributing this to the core. Perhaps a third argument with either an indexed array of column names or an associate array with key-value. The default is to update all columns. |
@mpskovvang absolutely, that was the follow up PR: #34712 😄 |
Did't see that. Spot on! Thanks! Much appreciated. 🙏 |
Does the feature support columns combination that uniquely identifies records? For example
departure - not unique If not can it be targeted by unique index instead of columns? |
Pulls in the work by @paras-malhotra in laravel/framework#34698 & laravel/framework#34712 for use in October CMS.
Added support for upsert in Laravel 8.10.0 laravel/framework#34698
Based off implementation done in laravel/framework#34698 and laravel/framework#34712.
Pulls in the work by @paras-malhotra in https:ithub.com/laravel/framework/pull/34698 & laravel/framework#34712 for use in October CMS. Co-authored-by: Ben Thomson <git@alfreido.com>
I have post meta table with fields [id, post_id, meta_key, meta_value]. i want to bulk update a meta values. But It repeatedly create a new row when using upsert method while updating form every time.
but while using updateOrCreate method it work fine.
|
@shankhadevpadam all databases except SQL Server require the columns in the second argument of the |
Only as reference, is important understand than
|
I noticed that Is that intended? My preference would be that |
Doesn't look like this is the case - anyone had luck with this? |
This statements applies to UPSERT as well? Because the created_at and updated_at did get filled by Eloquent |
This feature is badly needed. Do you have any solutions regarding this issue? |
Motivation
Laravel already supports bulk inserts, bulk insert/ignores. It's only logical to also support bulk upserts, which are already supported on frameworks such as Rails.
Bulk Inserts Already Supported
Bulk Upsert (This PR)
This PR supports bulk upserts (insert and on duplicate key, update) like so:
The first argument is the values to insert/update and the second argument is the column(s) that uniquely identify records. All databases except SQL Server require these columns to have a PRIMARY or UNIQUE index. This is similar to Rails
upsert_all
without thereturning
support.Eloquent upsets are also supported like so:
If the model uses the
updated_at
timestamp,upsert()
will also add the updated_at timestamp columns. Inspired from https://github.com/staudenmeir/laravel-upsert with some slight modifications. Ping @staudenmeir